﻿using UnityEngine;
using Entitas;
/// <summary>
/// Gameobjects with this script attached to it will automatically create a PlayerEntity. WARNING: there should only be one player entity
/// in a game session
/// </summary>
class PlayerView : MonoBehaviour,IBaseView,IPlayerPositionListener,IAnimator,IPlayerSpriteFlipListener
{
    public PlayerEntity Player;
    private Animator anim;
    private SpriteRenderer Spriterendere;
    private void Awake()
    {
        anim = GetComponent<Animator>();
        Spriterendere = GetComponent<SpriteRenderer>();
        EntitySetup();
    }

    private void EntitySetup()
    {
        // if there are no player entities on awake then create one
        if (Contexts.sharedInstance.player.count == 0)
        {
            Player = Contexts.sharedInstance.player.CreateEntity();
            Player.AddPosition(transform.position);
            Player.AddHitPoint(100);
            Player.AddDirection(Direction.Left);
            Player.AddEquippedWeapon(Weapon.Sword);
            Player.AddName(name);
            Player.AddSpeed(.1f);
            Player.AddInventory(new System.Collections.Generic.Dictionary<IItem, int>());
            Player.inventory.value.Add(Weapon.Sword, 1);
           Player.inventory.value.Add(Weapon.Bow,1);
           // Player.AddSpriteFlip(false, false);
            Player.AddPlayerPositionListener(this);
          //  Player.AddPlayerSpriteFlipListener(this);
            Player.AddBaseView(this);
            Player.AddAnimator(this);
        }
        else
        {
            //if there IS a player entity when awake is called for example when a scene switch then just add change entity position and
            // add a new object reference
            Player = Contexts.sharedInstance.player.GetEntities().SingleEntity<PlayerEntity>();
           Player.AddAnimator(this);
            Player.ReplacePosition(transform.position);
            Player.AddPlayerPositionListener(this);
           // Player.AddPlayerSpriteFlipListener(this);
            Player.AddBaseView(this);
            Player.AddAnimator(this);

        }
    }
   



    /// <summary>
    /// when the players collider collides with a enemy collider "EnemyZone" A battle is Engaged
    /// </summary>
    /// <param name="collision"></param>
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "EnemyZone")
        {
            Player.isBattleEngaged = true;
        }
    }
  
    /// <summary>
    /// When the player Exits From a collider tagged "EnemyZone" a battle is Disengaged.
    /// </summary>
    /// <param name="collision"></param>
    private void OnTriggerExit2D(Collider2D collision)
    {

        if (collision.tag == "EnemyZone")
        {
            Player.isBattleEngaged = false;
        }
    }
    /// <summary>
    /// Event which binds gameobject position with entity position. Ensures that when the Entity changes position the gameobject will
    /// follow suit.
    /// </summary>
    /// <param name="entity"></param>
    /// <param name="value"></param>

    public void OnPosition(PlayerEntity entity, EntitasVec3 value)
    {
        transform.position = value;
    }

    public void DestoryView()
    {
        Destroy(gameObject);
    }
    public string GetName()
    {
        return name;
    }
    public string GetTag()
    {
        return tag;
    }

    public bool GetAnimationState(string animation)
    {
        return anim.GetBool(animation);
    }

    public void PlayAnimation(string animation)
    {

        anim.Play(animation);
    }

    public void SetAnimationState(string animation, bool state)
    {
        anim.SetBool(animation, state);
    }

    public void StopAnimation()
    {
        anim.StopPlayback();
    }

    public void OnSpriteFlip(PlayerEntity entity, bool FlipX, bool FlipY)
    {
        Spriterendere.flipX = FlipX;
        Spriterendere.flipY = FlipY;
    }
}